<?php
// +----------------------------------------------------------------------
// | Date:2016年12月14日
// +----------------------------------------------------------------------
// | Author: EK_熊<1439527494@qq.com>
// +----------------------------------------------------------------------
// | Description: 此文件作用于****
// +----------------------------------------------------------------------

namespace Home\Controller;
use Think\Action;
use Think\Controller;
class BaseController extends Controller{

    protected $uid = '';
    protected $userInfo='';
    protected $searchBar = ['all'=>'全部','title'=>'标题','brand'=>'品牌','agency'=>'代理公司','keywords'=>'标签','country'=>'国家','summary'=>'广告语'];
    public function _initialize(){
        $this->_check_ip();
        $this->_check_user();
        $this->assign('user_name',$this->userInfo['name']);
        $this->assign("site", C('SITE_INFO'));
        $this->assign("searchBar", $this->searchBar);
        $this->assign("history_today",D('ProductHistory')->history_get_today($this->uid,8));//顶部历史记录预留
        $this->assign("count_comment",D('Comment')->count_unread($this->uid));//顶部历史记录预留
    }
    
    
    /**
     * 限制方法，仅限因赛伙伴内部IP使用！;
     * 跳转forbidden.html
     *
     * date:2016年12月13日
     * author: EK_熊
     */
    private function _check_ip(){
        $option = get_options('ip_limit');
        if ($option['status']) {
    
            $ipList = F('ip_white_list');
            if (!$ipList) {
                $ipList = M('Ip')->getField('ip',true);
            }
            $ip = get_real_ip();
            if(!in_array($ip, $ipList)){
                header("Location:forbidden.html");
    
            }
        }
    }
    
    /**
     * 用户登录检查
     * 
     * date:2017年1月5日
     * author: EK_熊
     */
    private function _check_user(){
        $userInfo = get_login_user();


        if (!$userInfo && !$this->_check_white_list()) {
            $this->redirect('Public/login',['jumpurl'=>base64_encode($_SERVER['REQUEST_URI'])]);
        }
        $userInfo['truename'] = $userInfo['name'];
        $this->userInfo = $userInfo; 
        $this->userInfo['name'] = !empty($userInfo['nickname']) ? $userInfo['nickname'] : $userInfo['name'];
        $this->uid = $userInfo['id'];
    }

    /**
     * @return bool登录权限白名单
     */
    private function _check_white_list(){
        $controller = CONTROLLER_NAME;
        $action = ACTION_NAME;
        $pathinfo = $_SERVER['PATH_INFO'];
        $whiteList = [
            'Index/detail',
            'Search/index',
            'Index/index',
            'Index/detail_mobile'
        ];

        foreach ($whiteList as $v){
            $path = $controller.'/'.$action;
            if ($v == $path){
                return true;
            }
        }
        return false;
    }
    
    /**
     * 限制方法，仅限因赛伙伴内部IP使用！;
     * 跳转forbidden.html
     * 
     * date:2016年12月13日
     * author: EK_熊
     */
//     private function _check_ip(){
//         $ipList = C('IP_WHITELIST');
//         if ($ipList) {
//             $ip = get_real_ip();
//             if(!in_array($ip, $ipList)){
//               header("Location:forbidden.html");
              
//             }
//         }
//     }


    public function _empty(){
        //把所有城市的操作解析到city方法
        $this->display('Base:empty');
        exit;
    }


}